package com.un.ebs.workflow.mapper;

import com.baomidou.mybatisplus.annotation.SqlParser;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.un.ebs.workflow.domain.WorkflowInstance;
import com.un.ebs.workflow.domain.WorkflowNodeInstance;
import com.un.ebs.workflow.domain.WorkflowNodeInstanceExt;
import com.un.ebs.workflow.enums.NodePassTypeEnum;
import org.apache.ibatis.annotations.*;
import org.springframework.test.context.jdbc.Sql;

import java.util.List;

/**
 * (WorkflowInstance)表数据库访问层
 *
 * @author liubo
 * @since 2020-08-23 10:00:02
 */
public interface WorkflowInstanceMapper extends BaseMapper<WorkflowInstance> {

    @SqlParser(filter = true)
    @Select("<script>" +
            "select * from v_workflow_node_instance_ext where node_pass_type = 0 and workflow_close_status = 0 and user_group_id in " +
            "<foreach collection='userGroupIds' item='item' open='(' separator=',' close=')'>" +
            "#{item}" +
            "</foreach>" +
            "</script>")
    List<WorkflowNodeInstanceExt> selectExtByUserGroup(@Param("userGroupIds") List<String> userGroupIds);

    @SqlParser(filter = true)
    @Select("select * from tb_workflow_instance where id = #{id}")
    WorkflowInstance selectByIdNotTenant(@Param("id") String id);

    @SqlParser(filter = true)
    @Select("select * from tb_workflow_node_instance where id = #{id}")
    WorkflowNodeInstance selectNodeByIdNotTenant(@Param("id") String noteInstanceId);


    @SqlParser(filter = true)
    @Select("select * from v_workflow_node_instance_ext where id = #{id}")
    WorkflowNodeInstanceExt selectNodeExtByIdNotTenant(@Param("id") String noteInstanceId);

    @SqlParser(filter = true)
    @Update("update tb_workflow_instance set close_status=1,close_time=getdate(),close_at=#{userId},close_at_name=#{personnelName},remark=#{remark},pass_status=#{isPass} where id = #{instanceId}")
    void closeWorkflowInstance(String instanceId, String userId, String personnelName, String remark, boolean isPass);

    @SqlParser(filter = true)
    @Insert("insert into tb_workflow_node_instance(id,workflow_instance_id,node_id,node_pass_type,parent_id,create_time,create_at,create_at_name,tenant_id) values(#{id},#{workflowInstanceId},#{nodeId},#{nodePassType},#{parentId},getdate(),#{createAt},#{createAtName},#{tenantId})")
    void createTask(@Param("id") String id, @Param("workflowInstanceId") String workflowInstanceId, @Param("nodeId") String nodeId, @Param("nodePassType") NodePassTypeEnum nodePassType, @Param("parentId") String parentId, @Param("createAt") String createAt, @Param("createAtName") String createAtName, @Param("tenantId") String tenantId);

    @SqlParser(filter = true)
    @Update("update tb_workflow_node_instance set node_pass_type=#{nodePassType}, complete_at=#{completeAt},complete_at_name=#{completeAtName},complete_time=getdate(),complete_remark=#{completeRemark} where id = #{id}")
    void completeTask(@Param("id") String id, @Param("nodePassType") NodePassTypeEnum nodePassType, @Param("completeAt") String completeAt, @Param("completeAtName") String completeAtName, @Param("completeRemark") String completeRemark);

}